package com.kantboot.system.user.dao.repository.entity;

import com.kantboot.system.user.domain.dto.SysPermissionSearchDTO;
import com.kantboot.system.user.domain.entity.SysPermission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SysPermissionRepository extends JpaRepository<SysPermission, Long> {

    @Query("""
    FROM SysPermission p
    LEFT JOIN SysPermissionRole pr
    ON p.code = pr.permissionCode
    """)
    List<SysPermission> getByRoleCode(@Param("roleCode") String roleCode);

    @Query("""
    FROM SysPermission p
    LEFT JOIN SysPermissionRole pr
    ON p.code = pr.permissionCode
    LEFT JOIN SysUserRole ur
    ON pr.roleCode = ur.roleCode
    WHERE ur.userId = :userId
    OR p.typeCode = 'notNeedLogin'
    OR p.typeCode = 'notNeed'
    """)
    List<SysPermission> getByUserId(@Param("userId") Long userId);

    @Query("""
    SELECT p.uri FROM SysPermission p
    LEFT JOIN SysPermissionRole pr
    ON p.code = pr.permissionCode
    LEFT JOIN SysUserRole ur
    ON pr.roleCode = ur.roleCode
    WHERE ur.userId = :userId
    OR p.typeCode = 'notNeedLogin'
    OR p.typeCode = 'notNeed'
    """)
    List<String> getUriListByUserId(@Param("userId") Long userId);

    @Query("""
    SELECT p.uri FROM SysPermission p
    WHERE p.typeCode = :typeCode
    """)
    List<String> getUriListByTypeCode(@Param("typeCode") String typeCode);

    /**
     * 根据用户id获取权限列表
     */
    @Query("""
    SELECT p FROM SysPermission p
    LEFT JOIN SysPermissionRole pr
    ON p.code = pr.permissionCode
    LEFT JOIN SysUserRole ur
    ON pr.roleCode = ur.roleCode
    WHERE ur.userId = :userId
    """)
    List<SysPermission> getListByUserId(@Param("userId") Long userId);

    /**
     * 根据用户id获取权限列表编码
     */
    @Query("""
    SELECT p.code FROM SysPermission p
    LEFT JOIN SysPermissionRole pr
    ON p.code = pr.permissionCode
    LEFT JOIN SysUserRole ur
    ON pr.roleCode = ur.roleCode
    WHERE ur.userId = :userId
    """)
    List<String> getCodeListByUserId(@Param("userId") Long userId);


    @Query("""
    FROM SysPermission p
    WHERE
    (:#{#param.typeCode} IS NULL OR p.typeCode = :#{#param.typeCode})
    AND (:#{#param.name} IS NULL OR p.name LIKE CONCAT('%',:#{#param.name},'%'))
    AND (:#{#param.code} IS NULL OR p.code LIKE CONCAT('%',:#{#param.code},'%'))
    AND (:#{#param.description} IS NULL OR p.description LIKE CONCAT('%',:#{#param.description},'%'))
    AND (:#{#param.uri} IS NULL OR p.uri LIKE CONCAT('%',:#{#param.uri},'%'))
    ORDER BY p.priority DESC
    """)
    Page<SysPermission> getBodyData(@Param("param") SysPermissionSearchDTO param, Pageable pageable);

    /**
     * 根据权限编码列表获取权限列表
     */
    @Query("""
    FROM SysPermission p
    WHERE p.code IN (:codeList)
    """)
    List<SysPermission> getListByCodeList(@Param("codeList") List<String> codeList);
}
